﻿#region Copyright (c) 2007-2011 SmartVault Corporation, all rights reserved.
// 
// http://www.opensource.org/licenses/mit-license.php
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion
using System;
using System.Windows.Forms;
using Interop.SVault.Client.SDK;

namespace SampleClient
{
    public partial class Form1 : Form, IContextStateMonitor
    {
        private ISmartVaultClientSDK _sdk;
        private IApplicationContext _context;
        private IContextNewItem _newItem;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            _sdk = Activator.CreateInstance(Type.GetTypeFromProgID("SmartVault.Client.SDK")) as ISmartVaultClientSDK;

            _sdk.Unlock("SampleCRMWinClient");
            _context = _sdk.Open("SampleCRM", "{20EE5F10-A808-462e-B50A-75D4D4E1605E}", "SmartVault Example Application");
            
            _context.AddStateMonitor(this);
            _context.HWndDialogOwner = this.Handle;
            _context.Provision();
            
            base.OnShown(e);
            tabControl1_Selected(tabControl1.SelectedTab,
                                 new TabControlEventArgs(tabControl1.SelectedTab, tabControl1.SelectedIndex,
                                                         TabControlAction.Selected));
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (_context != null)
            {
                _context.RemoveStateMonitor(this);
                _context.Close();
                _context = null;
            }
            base.OnHandleDestroyed(e);
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            IContextMessage msg;
            switch(e.TabPage.Name)
            {
                case "tabHome":
                    msg = _context.CreateMessage();
                    msg.SetRecordType("Home");
                    msg.Commit();
                    break;
                case "tabItem":
                    lbItems_SelectedIndexChanged(this, EventArgs.Empty);
                    break;
                case "tabNew":
                    msg = _context.CreateMessage();
                    _newItem = msg.SetNewItem("Customer");
                    msg.Commit();
                    btnDiscard.Enabled = btnCreate.Enabled = txtFirstName.Text.Length > 0 && txtLastName.Text.Length > 0;
                    break;
                case "tabNone":
                    _context.ClearCurrentItem();
                    break;
            }
        }

        private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            IContextMessage msg;
            if (lbItems.SelectedIndex < 0)
            {
                _context.ClearCurrentItem();
                btnDelete.Enabled = false;
            }
            else
            {
                btnDelete.Enabled = true;
                msg = _context.CreateMessage();
                msg.SetRecordTypeWithId("Customer", lbItems.Items[lbItems.SelectedIndex].ToString());
                msg.Commit();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(lbItems.SelectedIndex >= 0)
            {
                lbItems.Items.RemoveAt(lbItems.SelectedIndex);
                lbItems.SelectedIndex = -1;
            }
        }

        private void NameTextChanged(object sender, EventArgs e)
        {
            btnDiscard.Enabled = btnCreate.Enabled = txtFirstName.Text.Length > 0 && txtLastName.Text.Length > 0;
        }

        private void NameTextChanged(object sender, KeyEventArgs e)
        { NameTextChanged(sender, EventArgs.Empty); }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string name = String.Format("{0} {1}", txtFirstName.Text.Trim(), txtLastName.Text.Trim());
            lbItems.Items.Add(name);
            _newItem.Commit(name);

            lbItems.SelectedItem = name;
            tabControl1.SelectedTab = tabItem;
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            _newItem.AbandonItem();
            txtFirstName.Text = txtLastName.Text = String.Empty;
            tabControl1.SelectedTab = tabItem;
        }

        private void mnuAttach_Click(object sender, EventArgs e)
        {
            _context.Attach();
        }

        private void mnuView_Click(object sender, EventArgs e)
        {
            _context.ViewDocuments();
        }

        private void mnuNote_Click(object sender, EventArgs e)
        {
            _context.ViewNote();
        }

        void IContextStateMonitor.StateChanged(IContextState state)
        {
            mnuAttach.Enabled = state.CanAttach;
            mnuView.Enabled = state.CanViewDocuments;
            mnuView.ToolTipText = String.Format("{0} documents attached.", state.GetDocuments().Count);
            mnuNote.Enabled = state.CanViewNote;

            IContextNote note = state.GetNotes().TryGetByOrdinal(0);
            if (note == null)
                mnuNote.ToolTipText = String.Empty;
            else if (note.Urgent)
                mnuNote.ToolTipText = "You have an urgent note.";
            else
                mnuNote.ToolTipText = "You have a note.";
        }
    }
}
